﻿#region using

using System;
using System.Diagnostics;
using com.gmccadc.Utility;

#endregion

namespace MvcMall.Framework.Logging
{
    public class Log4NetLogger : ILogger
    {
        private readonly string _parentMethodDeclaringType;
        private readonly string _parentMethodName;

        public Log4NetLogger()
        {
            try
            {
                var st = new StackTrace(true);
                _parentMethodDeclaringType = st.GetFrame(0).GetMethod().DeclaringType.ToString();
                _parentMethodName = st.GetFrame(0).GetMethod().ToString();
            }
            catch (NullReferenceException)
            {
                _parentMethodDeclaringType = "找不到类型";
                _parentMethodName = "找不到方法";
            }
        }

        #region ILogger Members

        public void Info(string msg, string contextInfo)
        {
            Logger.LogInfo(_parentMethodDeclaringType, _parentMethodName, 0, msg, contextInfo);
        }

        public void Debug(string msg, string contextInfo)
        {
            Logger.LogDebug(_parentMethodDeclaringType, _parentMethodName, msg, contextInfo);
        }

        public void Perf(DateTime startTime, string contextInfo)
        {
            Logger.LogPerf(startTime, _parentMethodDeclaringType, _parentMethodName, contextInfo);
        }

        public void Warn(string msg, string contextInfo)
        {
            Logger.LogError(_parentMethodDeclaringType, _parentMethodDeclaringType, AppError.WARN, 9999, string.Empty,
                            msg, contextInfo);
        }

        public void Error(string msg, string contextInfo)
        {
            Logger.LogError(_parentMethodDeclaringType, _parentMethodName, AppError.EROR, 9999, string.Empty, msg,
                            contextInfo);
        }

        public void Error(Exception ex, string msg, string contextInfo)
        {
            Logger.LogError(_parentMethodDeclaringType, _parentMethodName, AppError.EROR, 9999, ex, msg, contextInfo);
        }

        public void Error(string moduleID, string funcCode, string msg, string contextInfo)
        {
            Logger.LogError(moduleID, funcCode, AppError.EROR, 9999, string.Empty, msg, contextInfo);
        }

        public void Fatal(string msg, string contextInfo)
        {
            Logger.LogError(_parentMethodDeclaringType, _parentMethodName, AppError.FATL, 9999, string.Empty, msg,
                            contextInfo);
        }

        public void Fatal(Exception ex, string msg, string contextInfo)
        {
            Logger.LogError(_parentMethodDeclaringType, _parentMethodName, AppError.FATL, 9999, ex, msg, contextInfo);
        }

        #endregion
    }
}